//Test Main

#include "vidPoker.h"
#include "BlackJ.h"
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void saveGame(float money,int numWins,int numLose);
void loadGame(float& money,int& numWins,int& numLose);
void showStats(float money, int numWins, int numLose);

void main()
{
     int choice;
     vidPoker poker;
     BlackJ blackJ;
     float money = 1000.00;
     int numWins =0;
     int numLose =0;
     int count = 0;
     

do{     
	if( count == 0){
         cout<<"Welcome to the CS232 Casino"<<endl<<endl;
		 cout <<"Would You like to load a previously saved game?(y/n) ";
		 char reply;
		 cin >> reply;
		 while(toupper(reply) != 'Y' && toupper(reply) != 'N'){
				cout << "Invalid entry, please enter another response(y/n): ";
				cin >> reply;
			}
			if (toupper(reply) == 'Y'){
				loadGame(money, numWins, numLose);
			}
	}

	 else{
		 system("cls");
         cout<<"Welcome back to the CS232 Casino lobby"<<endl;
	 }
         
     cout<<"You currently have  $"<<money<<"."<<endl;

     cout<< "Please select the game you want to play by entering a number (1-4) "<<endl<<endl;
     cout<<"\t\t\t 1. Video Poker"<<endl;
     cout<<"\t\t\t 2. BlackJack "<<endl;
     //cout<<"\t\t\t 3. Slots "<<endl;
     //cout<<"\t\t\t 4. Craps "<<endl;
	 cout<<"\t\t\t 3. View Statistics "<<endl;
     cout<<"\t\t\t 4. Exit Casino "<<endl;     

     cout << "Enter selection: ";
     cin>>choice;
	 
	
	while (choice < 1 || choice > 4)
	{
		 cout << "I'm sorry, thats not a valid menu choice, it has to be a number between (1-4)\n";
		 cout<<"\t\t\t 1. Video Poker"<<endl;
	     cout<<"\t\t\t 2. BlackJack "<<endl;
	     //cout<<"\t\t\t 3. Slots "<<endl;
	     //cout<<"\t\t\t 4. Craps "<<endl;
		 cout<<"\t\t\t 3. View Statistics "<<endl;
	     cout<<"\t\t\t 4. Exit Casino "<<endl;     

	     cout << "Enter selection: ";
	     cin>>choice;
	}
		
	
	 system("cls");
     if(choice == 1){
        poker.play(money, numWins, numLose);
        count++;
               }
     else if (choice == 2){
        blackJ.play(money,numWins,numLose);

        count++;
     }
     
	 else if(choice == 3){
		showStats(money, numWins, numLose);
		system("pause");
		count++;
		system("cls");
	 }
     
	 else if(choice == 4){
		 cout << "Would you like to save your game?" << endl << "!Warning - any previously saved data will be overwritten!(y/n): ";
		char reply;
		cin >> reply;
		while(toupper(reply) != 'Y' && toupper(reply) != 'N'){
			cout << "Invalid entry, please enter another response(y/n): ";
			cin >> reply;
		}
		if (toupper(reply) == 'Y'){
			saveGame(money, numWins, numLose);
		}

          cout<<"Thank you for visiting us today! Come again your money is always welcome!!"<<endl;
	 }

	 else
		  cout<<"That is not a valid choice!!"<<endl;
     
   }while(choice != 4);
}
/**********************************************************************************/

void saveGame(float money,int numWins,int numLose){
	ofstream out("CasinoStats.txt");

	cout << fixed << showpoint << setprecision(2);

	out << money << ' ' << numWins << ' ' << numLose;
	out.close();
}

void loadGame(float& money, int& numWins, int& numLose){
	ifstream in("CasinoStats.txt");

	in >> money >> numWins >> numLose;

	cout << money << ' ' << numWins << ' ' << numLose << endl << endl;

	in.close();
}

void showStats(float money, int numWins, int numLose){
	cout << "\t\tCurrent Statistics:" << endl << endl;
	cout << "Money: $" << money << endl;
	cout << "Wins: " << numWins << endl;
	cout << "Losses: " << numLose << endl << endl;
}